package com.eliansun.crawler.pageparse.sichuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eliansun.crawler.pageparse.AbstractPageProcess;
import net.dongliu.requests.Requests;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.Map;

/**
 * user: lxc
 * data:2019- 07-04-09
 * 药品终端网     ypzdw.com
 */
public class Ypzd extends AbstractPageProcess {

    int count = 0;
    /**
     *  商品列表界面异步加载数据url
     */
    public static final String GET_URL =  "https://ywapi.ypzdw.com/search/gateway/product/list?";

    /**
     * 访问商品详情界面url(不是详情界面数据加载url)
     */
    public static final String DETAIL_UTL = "https://www2.ypzdw.com/search/product/detail?";


    @Override
    public void process(Page page) {
        try{

            //列表界面
            if( ! page.getUrl().toString().contains("detail") ){
                Map<String,Object> parmes = new HashMap<String,Object>();
                String searchWhere =  super.wordKey;//page.getUrl().toString().split("\\?")[1].split("&")[0].split("=")[1];
                parmes.put("currPage","1");
                parmes.put("pageSize","25");
                parmes.put("key",searchWhere);
                parmes.put("stock","1");
                parmes.put("type","2");
                parmes.put("input",searchWhere);
                parmes.put("keywords",searchWhere);
                parmes.put("hasInventory","true");
                String result = Requests.get(GET_URL).params(parmes).send().readToText();
                if( ! "".equals( JSON.parseObject(result).getJSONObject("data").getString("data") ) ){
                    JSONArray data = JSON.parseObject(result).getJSONObject("data").getJSONArray("data");
                    for ( int i = 0; i < data.size(); i++ ) {
                        JSONObject obj = data.getJSONObject(i).getJSONObject("basic");
                        page.addTargetRequest(DETAIL_UTL  + "productId=" + obj.get("productId").toString() + "&productSpecId="+  obj.get("productSpecId").toString() );
                    }
                }

                //获取下一页放到队列中
                Object getTotalPage = JSON.parseObject( JSON.parseObject(result).get("data").toString()).get("totalPages");
                if( getTotalPage != null ){
                    //总页数
                    Integer totalPage =  Integer.parseInt( getTotalPage.toString() ) ;
                    for ( int i = 1; i < totalPage; i++ ) {
                         Map<String,Object> map = new HashMap<String,Object>();
                         map.put("currPage",""+(i+1)+"");
                         map.put("pageSize","25");
                         map.put("key",searchWhere);
                         map.put("stock","1");
                         map.put("type","2");
                         map.put("input",searchWhere);
                         map.put("keywords",searchWhere);
                         map.put("hasInventory","true");
                        String resultNext = Requests.get(GET_URL).params(map).send().readToText();
                        JSONArray dataNext = JSON.parseObject(resultNext).getJSONObject("data").getJSONArray("data");
                        for ( int j = 0; j < dataNext.size(); j++ ) {
                            JSONObject obj = dataNext.getJSONObject(j).getJSONObject("basic");
                            page.addTargetRequest(DETAIL_UTL  + "productId=" + obj.get("productId").toString() + "&productSpecId="+  obj.get("productSpecId").toString() );
                        }
                    }
                }
            }else{
                //详情界面
                Map<String,Object> map = new HashMap<String,Object>();
                Map<String, Object> resMap = new HashMap<String, Object>();
                String[] split = page.getUrl().toString().split("\\?");
                // 商品详情界面获取数据url
                String detailGetData = "https://ywapi.ypzdw.com/search/gateway/product/commonId/true";
                if(  split.length>1 ){
                    if( split[1].split("&")[1].split("=").length != 1){
                        detailGetData =  detailGetData.replace("commonId",""+ split[1].split("&")[1].split("=")[1]+"");
                    }else{
                        detailGetData =  detailGetData.replace("commonId",split[1].split("&")[0].split("=")[1]);
                    }
                }
                String result = Requests.get(detailGetData).params(map).send().readToText();
                JSONObject jsonObject = JSON.parseObject(result).getJSONObject("data").getJSONObject("basic");
                if( jsonObject != null ){
                    // 商品名称
                    resMap.put("goodsName",jsonObject.getString("generalName") );

                    //生产企业
                    resMap.put("sccj",jsonObject.getString("factoryName"));

                    //批准文号
                    resMap.put("zch",jsonObject.getString("allowNo"));


                    resMap.put("gg",jsonObject.getString("specification") + "/" + jsonObject.getString("unit"));

                }

                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("spec", resMap.get("gg"));
                resultMap.put("medicalDeviceNo", resMap.get("zch"));
                resultMap.put("productCompany", resMap.get("sccj"));
                resultMap.put("price", null);
                resultMap.put("brand", "");
                resultMap.put("productName", resMap.get("goodsName"));
                resultMap.put("executiveStandard", "");
                resultMap.put("nationality", "");
                resultMap.put("store", super.cTerrace);
                resultMap.put("url", page.getUrl());
                resultMap.put("version", "");
                page.putField("result", resultMap);
                count++;
                System.out.println(count);
            }
        }catch ( Exception e){
               e.printStackTrace();
        }
    }

    Site site = Site.me()
            //设置编码
            .setCharset("utf-8")
            //设置抓取间隔时间
            .setSleepTime(1000*3)
            //设置超时时间，单位毫秒
            .setTimeOut(1000*3)
            //设置重试间隔时间
            .setRetrySleepTime(1000*3)
            //设置重试次数
            .setRetryTimes(3);
    @Override
    public Site getSite() {
        return site;
    }



    public static void main(String[] args) {
        Spider.create( new Ypzd())
                .addUrl("https://www2.ypzdw.com/search/product/index?key=@key@&stock=1&type=2&input=@key@")
                //设置多个线程同时抓取数据
                .thread(1)
                .run();

    }

}
